package salutem.Telas;

import java.sql.SQLException;
import javax.swing.JPasswordField;
import salutem.DAO.UsuarioDAO;
import salutem.Utils.Msg;

/**
 * 
 * @author Renato Doretto
 */
public class TelaConfigSenha extends javax.swing.JDialog {

    private TelaPrincipal telaPrincipal;
    private UsuarioDAO daoUsuario;

    public TelaConfigSenha(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        this.daoUsuario = new UsuarioDAO();
        this.telaPrincipal = (TelaPrincipal) parent;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btSalvar = new javax.swing.JButton();
        txConfirmarSenha = new javax.swing.JPasswordField();
        txNovaSenha = new javax.swing.JPasswordField();
        txSenhaAtual = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ALTERAÇÃO DE SENHA");
        setResizable(false);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Senha Atual:");

        jLabel2.setText("Confirmar Senha:");

        jLabel3.setText("Nova Senha:");

        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        txConfirmarSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txConfirmarSenhaKeyTyped(evt);
            }
        });

        txNovaSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txNovaSenhaKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txConfirmarSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txNovaSenha, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txSenhaAtual, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(54, 54, 54))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txSenhaAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txNovaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txConfirmarSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCancelar)
                    .addComponent(btSalvar))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.setVisible(false);
        this.dispose();
}//GEN-LAST:event_btCancelarActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        this.salvar();
    }//GEN-LAST:event_btSalvarActionPerformed

    private void txNovaSenhaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txNovaSenhaKeyTyped
        if (((JPasswordField) evt.getSource()).getText().length() > 15) {
            evt.consume();
        }
    }//GEN-LAST:event_txNovaSenhaKeyTyped

    private void txConfirmarSenhaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txConfirmarSenhaKeyTyped
        if (((JPasswordField) evt.getSource()).getText().length() > 15) {
            evt.consume();
        }
    }//GEN-LAST:event_txConfirmarSenhaKeyTyped

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        this.telaPrincipal.repaint();
        this.repaint();
    }//GEN-LAST:event_formWindowGainedFocus

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaConfigSenha dialog = new TelaConfigSenha(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField txConfirmarSenha;
    private javax.swing.JPasswordField txNovaSenha;
    private javax.swing.JPasswordField txSenhaAtual;
    // End of variables declaration//GEN-END:variables

    private void salvar() {
        if (this.txSenhaAtual.getText().isEmpty()) {
            Msg.alerta(this, "Por favor, preencha a senha atual!!!");
            this.txSenhaAtual.requestFocus();
            return;
        }

        if (this.txNovaSenha.getText().isEmpty()) {
            Msg.alerta(this, "Por favor, preencha a nova senha!!!");
            this.txNovaSenha.requestFocus();
            return;
        }

        if (this.txConfirmarSenha.getText().isEmpty()) {
            Msg.alerta(this, "Por favor, preencha a confirmação da nova senha!!!");
            this.txConfirmarSenha.requestFocus();
            return;
        }

        if (!this.txNovaSenha.getText().equals(this.txConfirmarSenha.getText())) {
            Msg.alerta(this, "Senha e Confirmação não conferem!!!");
            this.txConfirmarSenha.requestFocus();
            this.txConfirmarSenha.selectAll();
            return;
        }
        try {
            if (daoUsuario.validarSenha(this.telaPrincipal.getUsuario().getIdUsuario(), this.txSenhaAtual.getText())) {
                daoUsuario.alterarSenha(this.telaPrincipal.getUsuario().getIdUsuario(), this.txNovaSenha.getText());
                Msg.informacao(this, "Senha alterada com sucesso!!!");
                this.setVisible(false);
                this.dispose();

            } else {
                Msg.erro(this, "Senha Atual Inválida!!!");
                this.txSenhaAtual.requestFocus();
                this.txSenhaAtual.selectAll();
            }
        } catch (SQLException ex) {
            Msg.erro(this, "Erro: " + ex.getMessage());
        }

    }
}
